home *** CD-ROM | disk | FTP | other *** search
- /* CPROG8-A.CPP - the bare bones of a menu system
-
- THIS VERSION OF THE FILE IS COMMENTED TO DEATH. SO THAT YOU CAN CLEARLY
- SEE HOW THE PROGRAM IS STRUCTURED, PROGRAM FILE CPROG8.CPP IS EXACTLY
- THE SAME CODE WITH NO EXPLANATORY TEXT
- */
-
-
- #include <stdio.h> // One of these .H files is needed for each
- #include <conio.h> // library function used
- #include <stdlib.h>
- #include <dos.h>
- #include <ctype.h>
-
- #define BEEP sound(300); delay(500); nosound();
-
- ////////////////////////////////////
- void pause(void) // Pause program until user hits a key
- {
- printf("\nPress a key...\n");
- while( kbhit() == 0 ) // While kbhit() doesn't report a key
- // has been pressed...
- ; // ...execute this empty statement
-
- (void)getch(); // getch() clears the unwanted character
- // from the keyboard buffer. Strictly
- // speaking, we need to specify what
- // happens to the returned value. The
- // preceding (void) tidies this up using
- // a feature called typecasting.
- // Unfortunately typecasting is probably
- // outside the scope of the series -
- // read up on it if you decide to take
- // up C or C++ in earnest. pause() will
- // work without the (void) - TCP seems
- // to be happy to forget the returned
- // value and not throw a wobbly.
- }
-
- ////////////////////////////////////
- void showmenu(void) // This function clears the screen and
- { // prints the menu text
- clrscr();
- printf("Press...\n");
- printf("1 ... for menu item 1\n");
- printf("2 ... for menu item 2\n");
- printf("3 ... for menu item 3\n");
- printf("\nQ ... to quit\n");
- }
-
- ////////////////////////////////////
- void action1(void) // Functions action1(), action2() and
- { // action3() are dummies that are called
- clrscr(); // up in response to menu items 1, 2 and 3
- printf("The program code for menu item 1\n");
- pause();
- }
-
- ////////////////////////////////////
- void action2(void)
- {
- clrscr();
- printf("The program code for menu item 2\n");
- pause();
-
- }
-
- ////////////////////////////////////
- void action3(void)
- {
- clrscr();
- printf("The program code for menu item 3\n");
- pause();
-
- }
-
- ////////////////////////////////////
- ////////////////////////////////////
- int main(void) // This program returns an integer value
- { // that appears in the DOS ERRORLEVEL.
- int kpress; // Stores the ASCII code for the key that the user pressed
-
- while( 1 ) // The entire program is a loop. Since a whole number
- // always evaluates as true, this loop never ends unless
- // something inside it causes the program to cease. This
- // action is activated by the user pressing the Q key.
-
- { // Opening brace for the instructions in the loop body
- showmenu(); // Clear the scren & display the menu text
- kpress=getch(); // Library function getch() waits for the
- // user to press a key and returns the ASCII
- // value of the typed character. This goes
- // into integer variable kpress.
- kpress=toupper(kpress); // Convert it to upper case to avoid
- // later testing for 'q' or 'Q'. Toupper() is
- // a library function detailed in the help
-
- switch( kpress ) // Choose from alternative courses of
- // action according to value in kpress
- { // opening brace of the SWITCH block
- case '1': // Could write case 49: since the
- // ASCII value for 1 is 49. C++ allows
- // you to use the actual character
- // between '' marks and will read it
- // as if you'd typed the raw number.
- action1(); // Do the actions associated
- // with this menu item.
- break; // Don't drop through to action2(); -
- // jump to the point immediately after
- // the closing brace of the SWITCH
- // block
-
- case '2':
- action2();
- break;
-
- case '3':
- action3();
- break;
-
- case 'Q': // kpress was earlier forced to upper
- // case. Otherwise it would be legal
- // to have case 'q': on the next line
- // to provide two routes into this
- // section
- exit(0); // Another library function -
- // terminates the program as if it
- // had hit the closing brace of main()
- // but, like return(), will pass a
- // value back. This appears in DOS's
- // ERRORLEVEL variable and can be used
- // to signal whether the program
- // terminated satisfactorily (0) or
- // some error occured (choose a
- // value that you then detail the
- // meaning of in your documentation).
- // alternatively, you can also use
- // ERRORLEVEL to pass back a specific
- // piece of data. The old getkey
- // program for batch file menus
- // is an example
-
- default: // If a key other than 1, 2, 3 or Q
- BEEP // was pressed, complain!
- } // Closing brace for SWITCH block
- } // Closing brace for the loop body - execution jumps
- // back to while( 1 )
- }
-
- /* Further note: This code sacrifices a little compactness for the sake of
- clarity. The variable kpress is not needed...
-
- ...
- ...
- showmenu();
- switch( toupper( getch() ) )
- {
- ...
- ...
-
- This works just as well. C++ evaluates the SWITCH line from the inside out:
-
- * Execute getch()
- * Make its return value the parameter to toupper()
- * Make the return value from toupper() the basis of the SWITCH()
-
- You would only need kpress if you wanted to save the keypress for use later
- in the program. You could then write this:
-
- switch( kpress=toupper( getch() ) )
-
- ...the return value from toupper() not only gets placed in kpress, but the
- whole kpress= expression evaluates to that number.
- */